package smartq.app;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class myDBClass extends SQLiteOpenHelper {
	

    // Database Version
    private static final int DATABASE_VERSION = 1;
 
    // Database Name
    private static final String DATABASE_NAME = "mydatabase";
 
    // Table Name
    private static final String TABLE_MEMBER = "members";

	public myDBClass(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		// Create Table Name
	    db.execSQL("CREATE TABLE " + TABLE_MEMBER + 
		          "(MemberID INTEGER PRIMARY KEY AUTOINCREMENT," +
		          " Name TEXT(100)," +
		          " Tel TEXT(100));");
	   
	    Log.d("CREATE TABLE","Create Table Successfully.");
	}
	

	// Select Data
	public String[] SelectData(String strMemberID) {
		// TODO Auto-generated method stub
		
		 try {
			String arrData[] = null;	
			
			 SQLiteDatabase db;
			 db = this.getReadableDatabase(); // Read Data
				
			 Cursor cursor = db.query(TABLE_MEMBER, new String[] { "*" }, 
					 	"MemberID=?",
			            new String[] { String.valueOf(strMemberID) }, null, null, null, null);
			 
			 	if(cursor != null)
			 	{
					if (cursor.moveToFirst()) {
						arrData = new String[cursor.getColumnCount()];
						/***
						 *  0 = MemberID
						 *  1 = Name
						 *  2 = Tel
						 */
						arrData[0] = cursor.getString(0);
						arrData[1] = cursor.getString(1);
						arrData[2] = cursor.getString(2);
					}
			 	}
			 	cursor.close();
			 	db.close();
				return arrData;
				
		 } catch (Exception e) {
		    return null;
		 }

	}
	
	// Delete Data
		public long DeleteData(String strMemberID) {
			// TODO Auto-generated method stub
			
			 try {
				
				SQLiteDatabase db;
	     		db = this.getWritableDatabase(); // Write Data
	     		
	     		/**
	     		 * for API 11 and above
				SQLiteStatement insertCmd;
				String strSQL = "DELETE FROM " + TABLE_MEMBER
						+ " WHERE MemberID = ? ";
				
				insertCmd = db.compileStatement(strSQL);
				insertCmd.bindString(1, strMemberID);
				
				return insertCmd.executeUpdateDelete();
				*
				*/
					
	     		long rows = db.delete(TABLE_MEMBER, "MemberID = ?",
			            new String[] { String.valueOf(strMemberID) });
	     		
	     		db.close();
	     		return rows; // return rows deleted.
					
			 } catch (Exception e) {
			    return -1;
			 }

		}
	
	// Insert Data
		public long InsertData(String strMemberID, String strName, String strTel) {
			// TODO Auto-generated method stub
			
			 try {
				SQLiteDatabase db;
	     		db = this.getWritableDatabase(); // Write Data
	     		
	     		/**
	     		 *  for API 11 and above
				SQLiteStatement insertCmd;
				String strSQL = "INSERT INTO " + TABLE_MEMBER
						+ "(MemberID,Name,Tel) VALUES (?,?,?)";
				
				insertCmd = db.compileStatement(strSQL);
				insertCmd.bindString(1, strMemberID);
				insertCmd.bindString(2, strName);
				insertCmd.bindString(3, strTel);
				return insertCmd.executeInsert();
				*/
				
				ContentValues Val = new ContentValues();
				Val.put("MemberID", strMemberID); 
				Val.put("Name", strName);
				Val.put("Tel", strTel);

				long rows = db.insert(TABLE_MEMBER, null, Val);

				db.close();
				return rows; // return rows inserted.
	           
			 } catch (Exception e) {
			    return -1;
			 }

		}
		
	// Show All Data
	public ArrayList<HashMap<String, String>> SelectAllData() {
		// TODO Auto-generated method stub
		
		 try {
			 
			 ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
			 HashMap<String, String> map;
			 
			 SQLiteDatabase db;
			 db = this.getReadableDatabase(); // Read Data
				
			 String strSQL = "SELECT  * FROM " + TABLE_MEMBER;
			 Cursor cursor = db.rawQuery(strSQL, null);
			 
			 	if(cursor != null)
			 	{
			 	    if (cursor.moveToFirst()) {
			 	        do {
			 	        	map = new HashMap<String, String>();
			 	        	map.put("MemberID", cursor.getString(0));
				 	        map.put("Name", cursor.getString(1));
				 	        map.put("Tel", cursor.getString(2));
				 	        MyArrList.add(map);
			 	        } while (cursor.moveToNext());
			 	    }
			 	}
			 	cursor.close();
			 	db.close();
				return MyArrList;
				
		 } catch (Exception e) {
		    return null;
		 }

	}
	
	

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBER);
        
        // Re Create on method  onCreate
        onCreate(db);
	}

}
